class ZonnarPlugin {

    static defaultOptions = {
        outputFile: 'assets.md'
    };

    constructor(option) {
        this.option = {
            ...ZonnarPlugin.defaultOptions, ...option
        };
    };

    apply(complier) {
        const pluginName = ZonnarPlugin.name;
        const { webpack } = complier;
        const { Compilation } = webpack;
        const {RawSource} = webpack.sources;
        complier.hooks.thisCompilation.tap(
            "ZonnarPlugin",
            (compilation, callback) => {
                compilation.hooks.processAssets.tap(
                    {
                      name: pluginName,
            
                      // Using one of the later asset processing stages to ensure
                      // that all assets were already added to the compilation by other plugins.
                      stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
                    },
                    (assets) => {
                      // "assets" is an object that contains all assets
                      // in the compilation, the keys of the object are pathnames of the assets
                      // and the values are file sources.
            
                      // Iterating over all the assets and
                      // generating content for our Markdown file.
                      const content =
                        '# In this build:\n\n' +
                        Object.keys(assets)
                          .map((filename) => `- ${filename}`)
                          .join('\n');
            
                      // Adding new asset to the compilation, so it would be automatically
                      // generated by the webpack in the output directory.
                      compilation.emitAsset(
                        this.option.outputFile,
                        new RawSource(content)
                      );
                    }
                  );
            }
        )
    }
}

module.exports = ZonnarPlugin;